import Vue from 'vue'
import VueRouter from 'vue-router'
import { Message } from 'element-ui'
import { routers } from './addroute'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'login',
    component: () => import('@/views/login.vue'),
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('@/views/home.vue'),
    children: routers,
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
})
router.beforeEach((to, from, next) => {
  let uid = localStorage.getItem('Authorization')
  if (uid || to.name == 'login') {
    next()
  } else {
    next({
      path: '/',
    })
    Message.error('请先登录后再进行操作！')
  }
})

export default router
